package api

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"local.public/errs"
	"local.public/reqs"
	"local.public/rsps"
)

func BindPingPongRouters(grp *gin.RouterGroup) {
	grp.GET("/ping", pingHandler)
	grp.POST("/ping", pingHandler)
	grp.POST("/err", errHandler)
	grp.POST("/header", getHeaderHandler)
	return
}

// pingHandler
// @Summary ping
// @Description  ping
// @Tags PingPong测试
// @Accept  json
// @Param data body interface{} true "参数"
// @Produce  json
// @Success 200 {object} ResponseInfo
// @Router /api/public/ping [post]
func pingHandler(ctx *gin.Context) {
	rsps.SuccessRspWithData(ctx, map[string]interface{}{
		"name":    "Magic Public",
		"message": "pong",
	})
}

func getHeaderHandler(ctx *gin.Context) {
	pHead := reqs.ExtractHeaderInfo(ctx)
	if pHead == nil {
		rsps.SuccessRspWithData(ctx, nil)
	} else {
		rsps.SuccessRspWithData(ctx, *pHead)
	}
}

func errHandler(ctx *gin.Context) {
	err := GetOneError()

	fmt.Println("code is" + strconv.Itoa(errs.GetErrCode(err)))
	fmt.Println("errshow is " + errs.GetErrShow(err))
	//err := GetNilError()
	//err := errors.New("heloo,err le")

	rsps.FailedRspWithError(ctx, err.Error(), err)
}

func GetOneError() error {
	return errs.NewCodeError(-1001, "这是一个错误", "")
}
